package com.shao.im.webSocket.utils;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelManager {

    private static Map<Integer, Channel> channelMap = new ConcurrentHashMap<>();
    private static Map<Channel, Integer> integerMap = new ConcurrentHashMap<>();

    /**
     * 存储上线的用户
     * @param id id
     * @param channel channel
     */
    public static void set(Integer id, Channel channel) {
        channelMap.put(id, channel);
        integerMap.put(channel, id);
    }

    /**
     * 通过id移除Channel
     * @param id id
     */
    public static void remove(Integer id) {
        Channel channel = channelMap.remove(id);
        integerMap.remove(channel);
    }

    /**
     * 通过channel找出id移除channel
     * @param channel channel
     */
    public static void remove(Channel channel) {
        Integer id = integerMap.remove(channel);
        channelMap.remove(id);
    }

    /**
     * 通过id获取Channel对象
     * @param id id
     * @return 返回
     */
    public static Channel get(Integer id) {
        return channelMap.get(id);
    }
}
